﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json.Linq;
using System.IO;
using Newtonsoft.Json;
using System.Web.Configuration;
using System.Data.SqlClient;
using JsonSubmitForm;
using JsonSubmitForm.App_Code;
using System.Globalization;

namespace JsonSubmitForm
{
    public partial class DataProcessor : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ProcessAjaxRequest();
        }

        private void ProcessAjaxRequest()
        {
            // execute request based on function type and save type (update / new record)
            if ((Request.QueryString["Save"] != null) && (Request.QueryString["Save"].ToString().Equals("1")))
            {
                SaveEmployeeData();
            }
            else if ((Request.QueryString["Save"] != null) && (Request.QueryString["Save"].ToString().Equals("2")))
            {
                UpdateEmployeeData();
            }
            else if ((Request.QueryString["function"] != null) && (Request.QueryString["function"].ToString().Equals("getEmpList")))
            {
                GetEmployeeData();
            }
            else if ((Request.QueryString["function"] != null) && (Request.QueryString["function"].ToString().Equals("getEmpId")) && (null != Request.QueryString["id"]))
            {
                GetEmployeeByID(Convert.ToInt32(Request.QueryString["id"].ToString()));
            }
            else if ((Request.QueryString["function"] != null) && (Request.QueryString["function"].ToString().Equals("delEmp")) && (null != Request.QueryString["id"]))
            {
                DeleteEmployee(Convert.ToInt32(Request.QueryString["id"].ToString()));
            }
            
        }

        private void DeleteEmployee(int id)
        {
            EmployeeData emp = new EmployeeData();
            Entity.Status stat = emp.DeleteEmployee(id);

            // send response using json
            string json = JsonConvert.SerializeObject(stat, Formatting.Indented);
            Response.Write(json);
        }

        private void GetEmployeeByID(int id)
        {
            EmployeeData emp = new EmployeeData();
            Entity.Employee employee = emp.GetEmployeeByID(id);

            // send response using json
            string json = JsonConvert.SerializeObject(employee, Formatting.Indented);
            Response.Write(json);
        }

        private void GetEmployeeData()
        {
            List<Entity.Employee> empList = new List<Entity.Employee>();
            EmployeeData emp = new EmployeeData();

            empList = emp.GetAllEmployee();
            
            // send response using json
            string json = JsonConvert.SerializeObject(empList, Formatting.Indented);
            Response.Write(json);
        }

        private void SaveEmployeeData()
        {
            // data passed in as JSON format
            StreamReader sr = new StreamReader(Request.InputStream);

            string line = "";
            line = sr.ReadToEnd();

            // this is all you need to parse JSON string into a JObject
            JObject jo = JObject.Parse(line);

            Entity.Status stat = new Entity.Status();

            Entity.Employee objEmployee = new Entity.Employee();
            objEmployee.FirstName = (string)jo["FirstName"];
            objEmployee.MiddleName = (string)jo["MiddleName"];
            objEmployee.LastName = (string)jo["LastName"];
            objEmployee.Title = (int)jo["Title"];
            objEmployee.Position = (string)jo["Position"];
            objEmployee.EmployeeID = (string)jo["EmployeeID"];

            DateTime jd = DateTime.MinValue;

            if (DateTime.TryParse((string)jo["JoinedDate"], CultureInfo.InvariantCulture, DateTimeStyles.None, out jd))
            {
                objEmployee.JoinedDate = jd;
            }

            EmployeeData emp = new EmployeeData();
            stat = emp.InsertEmployee(objEmployee);

            // send response using json
            string json = JsonConvert.SerializeObject(stat, Formatting.Indented);
            Response.Write(json);

        }

        private void UpdateEmployeeData()
        {
            // data passed in as JSON format
            StreamReader sr = new StreamReader(Request.InputStream);

            string line = "";
            line = sr.ReadToEnd();

            // this is all you need to parse JSON string into a JObject
            JObject jo = JObject.Parse(line);

            Entity.Status stat = new Entity.Status();

            Entity.Employee objEmployee = new Entity.Employee();

            objEmployee.Id = Convert.ToInt32((string)jo["id"]);
            objEmployee.FirstName = (string)jo["FirstName"];
            objEmployee.MiddleName = (string)jo["MiddleName"];
            objEmployee.LastName = (string)jo["LastName"];
            objEmployee.Title = (int)jo["Title"];
            objEmployee.Position = (string)jo["Position"];
            objEmployee.EmployeeID = (string)jo["EmployeeID"];
            DateTime jd = DateTime.MinValue;

            if (DateTime.TryParse((string)jo["JoinedDate"], CultureInfo.InvariantCulture, DateTimeStyles.None, out jd))
            {
                objEmployee.JoinedDate = jd;
            }

            EmployeeData emp = new EmployeeData();
            stat = emp.UpdateEmployee(objEmployee);

            // send response using json
            string json = JsonConvert.SerializeObject(stat, Formatting.Indented);
            Response.Write(json);

        }

    }
}